package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.Untils.TokenUtils;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Notice;
import com.example.springboot.entity.User;
import com.example.springboot.service.NoticeService;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 雒鑫
 * Java Web
 */
@CrossOrigin
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    //通过该注解 拿到spring容器当中userService的一个实例
    //通过该实例调用方法
    NoticeService noticeService;
    @Autowired
    UserService userService;

    /*
    新增用户信息
     */
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice) {
        User currentUser = TokenUtils.getCurrentUser();//获取当前登录的用户信息
        notice.setUserid(currentUser.getId());;
        notice.setTime(DateUtil.now());//获取当前的时间
        noticeService.save(notice);
        return Result.success();
    }

    /*
   修改信息
    */
    @PutMapping("/update")
    public Result update(@RequestBody Notice notice) {
        noticeService.updateById(notice);
        return Result.success();
    }

    /*
   删除单个信息
    */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        noticeService.removeById(id);
        return Result.success();
    }

    /*
   批量删除信息
    */
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        noticeService.removeBatchByIds(ids);
        return Result.success();
    }

    /*
  查询信息
   */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Notice> users = noticeService.list(new QueryWrapper<Notice>().orderByDesc("id"));
        return Result.success(users);
    }

    /*
根据id 查询信息
  */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {

        Notice notice = noticeService.getById(id);
        User user = userService.getById(notice.getUserid());
        if (user!=null){
            notice.setUser(user.getName());
        }
        return Result.success(notice);
    }

    /*
    分页多条件模糊查询
    计算limit:(pageNum-1)*pageSize
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(
            @RequestParam Integer pageNum,//当前是那一页（当前的页码）
            @RequestParam Integer pageSize,//当前页有多少条
            @RequestParam String title) {
        /*
        分页一般返回两个数据: 一个是用户的列表 一个是用户的总数
         */
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>().orderByDesc("id");
        queryWrapper.like(StrUtil.isBlank(title), "title", title);
        Page<Notice> page = noticeService.page(new Page<>(pageNum, pageSize), queryWrapper);
        List<Notice> records = page.getRecords();
       // List<User> list = userService.list();
        for (Notice record : records) {
            Integer authorid = record.getUserid();
            User user = userService.getById(authorid);
            //String author = list.stream().filter(u -> u.getId().equals(authorid)).findFirst().map(User::getName).orElse("");
            //record.setAuthor(author);
            if (user!=null){
                record.setUser(user.getName());
            }
        }


        return Result.success(page);
    }
    /**
     * 查询用户公告
     * @return 用户公告
     */
    @GetMapping("/selectUserData")
    public Result selectUserData() {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<Notice>().orderByDesc("id");
        queryWrapper.eq("open", 1); // 用户只能看到公开的公告数据
        List<Notice> userList = noticeService.list(queryWrapper);
        return Result.success(userList);
    }



}
